<template class="bg">
	<view class="contaner">
		<view class="mine">个人中心</view>
		<block v-if="data.code=='0'">
			<view class="auth_ul">
				<view class="message" @click="toInfo">
					<uv-avatar size="124rpx" class="image" :src="info.photoUrl"></uv-avatar>
				</view>
				<view class="rit" @click="toInfo">
					<view v-if="info.nick" class="nickName">{{info.nick}}</view>
					<view v-else class="nickName">游客</view>
					<view v-if="info.provinceName" class="adress">地址: {{info.provinceName}}{{info.cityName}}</view>
					<view v-else class="adress">地址: 未知</view>
				</view>
				<!-- <view class="icon-right">
					<uni-icons v-if="!isLogin" type="right" size="15" @click="login"></uni-icons>
				</view> -->
			</view>
		</block>
		<block v-else-if="data.code=='1'">
			<view class="auth_ul">
				<view class="message">
					<image class="image" src="../../static/默认头像.png"></image>
				</view>
				<view class="rit">
					<view class="nickName">授权登录</view>
				</view>
			</view>
		</block>
		<view class="card">
			<uni-card class=card-shadow :is-shadow="true" shadow="0px 0px 16px 1px rgba(0, 0, 0, 0.08)">
				<uni-list :border="false">
					<!-- 	<uni-list-item class="card-font" title="扫一扫" @click="camera" link showArrow :border="false"
						:thumb="scaners" thumb-size="base" />
					<uni-list-item class="card-font" title="重置位置" @click="postation" link showArrow :border="false"
						:thumb="positions" thumb-size="base" />
					<uni-list-item class="card-font" title="关于我们" @click="about" link showArrow :border="false"
						:thumb="ours" thumb-size="base" /> -->
					<uni-list-item class="card-font" :title="'用户信息：' + info.nick"  link showArrow :border="false"
						:thumb="scaners" thumb-size="base" />
					<uni-list-item class="card-font" :title="'位置信息：' + info.provinceName" link showArrow :border="false"
						:thumb="positions" thumb-size="base" />
					<uni-list-item class="card-font" title="重置位置" @click="postation" link showArrow :border="false"
						:thumb="ours" thumb-size="base" />
					<uni-list-item class="card-font" :title="titleUser" @click="out" link :border="false" :thumb="outs"
						thumb-size="base" />

				</uni-list>
			</uni-card>
		</view>
	</view>

</template>

<script setup>
	import {
		onMounted,
		reactive,
		ref
	} from "vue";
	import {onShow} from '@dcloudio/uni-app'
	import useGetGlobalProperties from '@/util/useGlobal.js'
	import useUpload from "@/hooks/useUpload";
	const urls = useGetGlobalProperties().$https;
	const url = useGetGlobalProperties().$httpUrl;
	// const ours = url + '/position.png';
	// const scaners = url + '/scaner.png';
	// const positions = url + '/position.png';
	// const outs = url + '/out.png';
	const ours = url + '/reflushlocation.png';
	const scaners = url + '/usermessage.png';
	const positions = url + '/location.png';
	const outs = url + '/loginout.png';
	const data = reactive({
		code: '0',
		avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
	});

	// const chooseImage = () => {
	//   uni.chooseImage({
	//     count: 1,
	//     sizeType: ['original', 'compressed'],
	//     success: (res) => {
	//       // 图片选择成功的回调
	//       // res中返回一个图片本地的临时地址
	//       console.log(res);
	//       // 将选择的图片地址赋值给avatarUrl
	//       data.avatarUrl = res.tempFilePaths[0];
	//     }
	//   });
	// };

	const data1 = ref({
		avatarUrl: 'https://img1.baidu.com/it/u=1310564963,1641173348&fm=253&fmt=auto&app=120&f=JPEG?w=1280&h=800'
	});
	const nickName = ref({
		nickName: '一片雪花'
	});
	const adress = ref({});
	const onChooseAvatar = (e) => {
		// data1.value = e.detail;
		useUpload(e.detail.avatarUrl).then(res => {
			data1.value.avatarUrl = res
		})
		console.log(e)
	}
	const camera = () => {
		console.log(11111);
		uni.scanCode({
			success: (res) => {
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
			}
		});
	}
	const postation = () => {
		console.log(2222);
		uni.scanCode({
			success: (res) => {
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
				console.log('路径：', res.path);
				let arry =  res.result.split('?');
				let scene = arry[1].split('=');
				let slice = scene[1];
				console.log(slice);
				uni.login({
					provider: 'weixin',
					onlyAuthorize: true,
					success: res => {
						let code = res.code;
						console.log(code, '--##----')
						uni.request({
							// url: 'https://weixin.shuzinongyue.com:8942/userInfo/wxlogin',
							// url: 'http://61.163.243.13:8944/userInfo/wxlogin',
							url: 'https://wechat.shuzinongyue.com:8922/userInfo/wxlogin',
							method: 'post',
							data: {
								code: code,
								location: slice
							},
							success: (res) => {
								uni.setStorageSync('userInfo', res.data.data);
							}
						})
					}
				})
				
			}
		});
	}
	const about = () => {
		uni.navigateTo({
			url: "/pages/admin/about"
		})
	}

	const toInfo = () => {
		uni.navigateTo({
			url: '/pages/admin/info'
		})
	}

	const login = () => {
		uni.navigateTo({
			// url: "/pages/admin/login"
		})
	}
	const titleUser = ref();
	const isLogin = ref();
	const out = () => {

		let r = uni.getStorageSync('userInfo');
		console.log('r', r)

		if (r) {
			isLogin.value = false;
			uni.removeStorageSync('userInfo');
			titleUser.value = '登录'
			init();
			data.code == '1';
		} else {
			uni.showModal({
				title: '提示',
				content: '是否要授权登录?',
				success: function(res) {
					if (res.confirm) {
						isLogin.value = true;
						//location.reload();
						loginUser();
						titleUser.value = '退出登录'
						data.code == '0';
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});

		}

		// init();
		//location.reload();

	}
	const loginUser = () => {
		uni.showLoading({
			title: '正在加载中'
		})
		let code = ''

		console.log('11111')
		uni.login({
			provider: 'weixin',
			onlyAuthorize: true,
			success: res => {
				console.log(res, '登录成功')
				code = res.code;
				console.log("code=" + res.code);
				uni.request({
					// url:  'https://weixin.shuzinongyue.com:8942/userInfo/getOpenId/' + code,
					// url:  'http://61.163.243.13:8944/userInfo/getOpenId/' + code,
					url:  'https://wechat.shuzinongyue.com:8922/userInfo/getOpenId/' + code,
					method: 'GET',
					success: (res) => {
						if (res.data.data) {
							console.log('res.data.look', res.data.data)
							uni.setStorageSync('userInfo', res.data.data);
							let r = res.data.data;
							info.value = r;
							info.value.nick = r.nick;

							console.log('user', r)
							console.log('res.data', res.data.data)
							titleUser.value = '退出登录';
						} else {
							uni.showToast({
								title: '请登录后进行阅读',
								icon: 'success',
								duration: 1500,
								mask: false
							});
							uni.redirectTo({
								// url: '/pages/admin/login'
							})
						}
						uni.hideLoading();
					}
				})
			}
		})
	}
	const info = ref({
		nick: '游客',
		provinceName: '未知',
		cityName: '',
		photoUrl: ''
	})
	const init = () => {
		let r = uni.getStorageSync('userInfo');
		console.log('dengluluoji', r)
		if (r == null || r.openId == null || r == '') {
			info.value = {
				nick: '游客',
				provinceName: '未知',
				cityName: '',
				photoUrl: ''
			}
			//loginUser();
			titleUser.value = '登录'
			return;
		}
		titleUser.value = '退出登录'
		info.value = r;
		console.log('info.value', info.value);
	}
	onShow(() => {
		init();
		// loginUser();
	})
</script>

<style>
	.icon-right {
		position: relative;
		left: 265rpx;
	}

	.card-font {
		position: relative;
		top: 5rpx;
		right: 30rpx;
		margin-bottom: 20rpx;
	}

	.contaner {
		font-family: my-font !important;
		margin-top: 120rpx;
		margin-left: 20rpx;
	}

	.card {
		width: 700rpx;
		height: 530rpx;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		margin-top: 50rpx;

	}

	.mine {
		font-size: 36rpx;
		font-family: my-font !important;
		font-weight: 500;
		color: #000000;
		position: relative;
		bottom: 20rpx;
	}

	.message {
		align-items: center;
		padding: 24rpx 0;
		border-radius: 50%;
	}

	.avatar-wrapper {
		background: none;
		padding: 0;
		width: 124rpx;
		height: 124rpx;
		border-radius: 80%;
		border: none;
		background-color: transparent;
		padding: 0;
	}

	.image {
		width: 124rpx;
		height: 124rpx;
		object-fit: cover;
	}

	.auth_ul {
		display: flex;
		align-items: center;
		margin-left: 10rpx;
	}

	.rit {
		margin-left: 30rpx;
		font-family: my-font !important;
	}

	.nickName {
		max-width: 230rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		height: 52rpx;
		font-size: 36rpx;
		font-weight: 500;
		color: #1D2129;
		line-height: 52rpx;

	}

	.adress {
		width: 216rpx;
		height: 40rpx;
		font-size: 24rpx;
		font-weight: 400;
		color: #86909C;
		line-height: 40rpx;
	}

	uni-button:after {
		width: 0%;
		height: 0%;
	}
</style>